﻿//
// Syncer.cs: Abstract definition of a syncer, which syncs some sort of
// data to a device.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace S9PalCommon
{
    /// <summary>
    /// Abstract definition of a syncer, which syncs some sort of data to a device. Inherits from Worker.
    /// </summary>
    public abstract class Syncer : Worker
    {
        #region Fields

        // Static variables
        private static DriveInfo drive;

        #endregion

        #region Properties

        /// <summary>
        /// The drive that is being synced to.
        /// </summary>
        public static DriveInfo Drive
        {
            get
            {
                return drive;
            }
            set
            {
                drive = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        ///  Attempt to locate a drive for an S9 device.
        /// </summary>
        public static void UpdateDrive()
        {
            String[] drives = Environment.GetLogicalDrives();
            FileInfo didFile;

            for (int i = 0; i < drives.Length; i++)
            {
                //try
                //{
                    DriveInfo di = new DriveInfo(drives[i]);
                    didFile = new FileInfo(di.RootDirectory.ToString() + "DID.bin");
                    if (didFile.Exists)
                    {
                        drive = di;
                        return;
                    }
                //}
                //catch
                //{

                //}
            }

            drive = null;
        }

        #endregion

        #region Protected Methods

        protected override void WorkThreadMethod()
        {
#if (!DEBUG)
            try
            {
#endif
                // Call the job's entry point.
                OnStart();

                // If the worker is still set to Active, change it's status to Finished.
                if (Status == WorkerStatus.Active)
                    Status = WorkerStatus.Finished;
#if (!DEBUG)
            }
            catch (DirectoryNotFoundException ex)
            {
                UpdateDrive();
                if (Drive == null)
                {
                    Status = WorkerStatus.Stopped;
                }
                else
                {
                    Global.Logger.Fatal(ex.ToString());
                    MessageBox.Show(ex.Message.ToString(), ex.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw ex;
                }
            }
            catch (FileNotFoundException ex)
            {
                UpdateDrive();
                if (Drive == null)
                {
                    Status = WorkerStatus.Stopped;
                }
                else
                {
                    Global.Logger.Fatal(ex.ToString());
                    MessageBox.Show(ex.Message.ToString(), ex.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                Global.Logger.Fatal(ex.ToString());
                MessageBox.Show(ex.Message.ToString(), ex.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
#endif
        }

        #endregion
    }
}
